package com.trickytriads.viewmodels
{
	import com.trickytriads.events.LoadNewsEvent;
	import com.trickytriads.events.LoadingEvent;
	import com.trickytriads.model.NewsData;
	import com.trickytriads.viewmodels.interfaces.I_NewsViewModel;
	
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;

	/**
	 * Viewmodel for the NewsView state.
	 **/
	[Event(name="newsLoadRequest", type="com.trickytriads.events.LoadNewsEvent")]
	[Event(name="loadGeneric", type="com.trickytriads.events.LoadingEvent")]
	[Event(name="loadGenericDone", type="com.trickytriads.events.LoadingEvent")]
	[ManagedEvents("newsLoadRequest,loadGeneric,loadGenericDone")]
	public class NewsViewModel extends EventDispatcher implements I_NewsViewModel
	{
		// ArrayCollection of news data.
		[ArrayElementType("com.trickytriads.model.NewsData")]
		private var _newsDataCollection:ArrayCollection;
		// Current news data that is being displayed.
		private var _currentNewsData:NewsData;
		
		// Sends out a news load request.
		public function newsLoadRequest (id:int) : void
		{
			// dispatch a generic load event to get a popup window
			dispatchEvent(new LoadingEvent(LoadingEvent.LOAD_GENERIC, "Contacting server"));
			dispatchEvent(new LoadNewsEvent(LoadNewsEvent.NEWS_LOAD_REQUEST, id));
		}
	
		// Function that is called whenever the news loading is done.
		[MessageHandler(selector="newsLoadDone")]
		public function newsLoadDone (evt:LoadNewsEvent) : void
		{
			trace("News loaded, storing.");			
			if (evt.news != null)
				currentNewsData = evt.news;
				
			// Remove the pop-up
			dispatchEvent(new LoadingEvent(LoadingEvent.LOAD_GENERIC_DONE, ""));				
		}
		
		// Getters/setters
		[Bindable]
		public function set currentNewsData(v:NewsData):void { _currentNewsData = v; }
		public function get currentNewsData():NewsData { return _currentNewsData; }	
		
	}
}